{{ define "main" }}

<section class="hero" style="background-image:linear-gradient(rgba(20,30,38, 0.9), rgba(24,44,54, 0.8)),
url('{{.Params.Image}}');">
  <header class="container">
    <hgroup>
      <h1>{{ .Title }}</h1>
      <h2>{{ .Description }}</h2>
    </hgroup>
  </header>
</section>

{{- $currentPage := . }}
<div class="container">
  <div class="grid grid-post">
    {{- $pages := union .RegularPages .Sections }}

    {{- if .IsHome | eq .Section "blog"}}
    {{- $pages = where site.RegularPages "Type" "in" site.Params.mainSections }}
    {{- end }}
    {{- $paginator := .Paginate $pages }}    
{{ range $paginator.Pages }}
    <a href="{{ .RelPermalink }}">
      <article class="text-center">
        {{if .Params.Cover.Image}}
        <figure>
          <img
            src="{{.Params.Cover.Image | absURL}}"
            width="600"
            height="400"
            layout="responsive"
          ></img>
        </figure>
        {{end}}
        <hgroup>
          <h3>{{ .Title }}</h3>
          <hr />
          <p>
            {{ .Description | plainify | htmlUnescape | truncate 100 }}{{ if
            .Truncated }}...{{ end }}
          </p>
        </hgroup>
        <small> {{- partial "post_meta.html" . -}} </small>
      </article>
    </a>
    {{ end }}
  </div>
  <!-- pagination -->
  <div>
    <div>
      {{ $paginator := .Paginator }}
      <!-- Number of links either side of the current page. -->
      {{ $adjacent_links := 2 }}
      <!-- $max_links = ($adjacent_links * 2) + 1 -->
      {{ $max_links := (add (mul $adjacent_links 2) 1) }}
      <!-- $lower_limit = $adjacent_links + 1 -->
      {{ $lower_limit := (add $adjacent_links 1) }}
      <!-- $upper_limit = $paginator.TotalPages - $adjacent_links -->
      {{ $upper_limit := (sub $paginator.TotalPages $adjacent_links) }}
      <!-- If there's more than one page. -->
      {{ if gt $paginator.TotalPages 1 }}
      <nav class="container" aria-label="Page navigation">
        <ul>
          <!-- Previous page. -->
          {{ if $paginator.HasPrev }}
          <li>
            <a href="{{ $paginator.Prev.URL }}"> &laquo; Previous </a>
          </li>
          {{ else }}
          <li disabled="">&laquo; Previous</li>
          {{ end }}
        </ul>
        <ul>
          <!-- Page numbers. -->
          {{ range $paginator.Pagers }} {{ $.Scratch.Set "page_number_flag"
          false }}
          <!-- Advanced page numbers. -->
          {{ if gt $paginator.TotalPages $max_links }}
          <!-- Lower limit pages. -->
          <!-- If the user is on a page which is in the lower limit.  -->
          {{ if le $paginator.PageNumber $lower_limit }}
          <!-- If the current loop page is less than max_links. -->
          {{ if le .PageNumber $max_links }} {{ $.Scratch.Set "page_number_flag"
          true }} {{ end }}
          <!-- Upper limit pages. -->
          <!-- If the user is on a page which is in the upper limit. -->
          {{ else if ge $paginator.PageNumber $upper_limit }}
          <!-- If the current loop page is greater than total pages minus $max_links -->
          {{ if gt .PageNumber (sub $paginator.TotalPages $max_links) }} {{
          $.Scratch.Set "page_number_flag" true }} {{ end }}
          <!-- Middle pages. -->
          {{ else }} {{ if and ( ge .PageNumber (sub $paginator.PageNumber
          $adjacent_links) ) ( le .PageNumber (add $paginator.PageNumber
          $adjacent_links) ) }} {{ $.Scratch.Set "page_number_flag" true }} {{
          end }} {{ end }}
          <!-- Simple page numbers. -->
          {{ else }} {{ $.Scratch.Set "page_number_flag" true }} {{ end }}
          <!-- Output page numbers. -->
          {{ if eq ($.Scratch.Get "page_number_flag") true }}
          <li class="page-item{{ if eq . $paginator }} active {{ end }}">
            <a href="{{ .URL }}"> {{ .PageNumber }} </a>
          </li>
          {{ end }} {{ end }}
        </ul>
        <ul>
          <!-- Next page. -->
          {{ if $paginator.HasNext }}
          <li>
            <a href="{{ $paginator.Next.URL }}"> Next &raquo; </a>
          </li>
          {{ else }}
          <li disabled>Next &raquo;</li>
          {{ end }}
        </ul>
      </nav>
      {{ end }}
    </div>
  </div>
  <!-- pagination -->
</div>
{{ end }}
